/**
*  \file driver_present.c
*
*  \brief
*
*  \author Michael J. Hohmann  <michael.hohmann@linde-mh.de>
*
*
*/

/*===============================================[ private includes  ]================================================*/
#include "driver_present.h"
#include "../assertor/assertor.h"
#include "../pal/pal_mem.h"

/*===============================================[ private defines   ]================================================*/

/*===============================================[ private datatypes ]================================================*/

/** Private variables */
struct driver_present_private
{
	enum trilean current_state;
};

/*===============================================[ private variables ]================================================*/
static struct driver_present_private driver_present;

/*===============================================[ private functions ]================================================*/

/*================================================[ inline functions ]================================================*/

/*================================================[ public functions ]================================================*/
RC driver_present_initialize( void )
{
   pal_memzero( &driver_present, sizeof driver_present);
   driver_present.current_state = trilean_unknown;
   return RC_SUCCESS;
}

enum trilean driver_present_process( S16 seat1, S16 seat2, S16 pedal, BO dual_channel )
{
	enum trilean rv = driver_present.current_state;
	S16 debounce_time=0;

	if( pedal==0 )
	{
		debounce_time=5;
	}
	else
	{
		debounce_time=30;
	}

	if( dual_channel )
	{

	}
	else
	{
		switch(  driver_present.current_state )
		{
			case trilean_true:
			{
				if( seat1 < - debounce_time)
				{
					rv=trilean_false;
				}
				break;
			}
			case trilean_false:
			{
				if( seat1 >  debounce_time )
				{
					rv=trilean_true;
				}
				break;
			}
			default:
			{
				if( seat1 >  debounce_time )
				{
					rv=trilean_true;
				}
				else
				{
					if( seat1 <  debounce_time )
					{
						rv=trilean_false;
					}
				}
				break;
			}
		}
	}

	driver_present.current_state=rv;
	return rv;
}

/*===============================================[ private functions ]================================================*/


/***********************************************************************************************************************
*                             Copyright 2020 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
